/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.ContactAutoComplete;
/*     */ import com.zimbra.cs.mailbox.ContactAutoComplete.AutoCompleteResult;
/*     */ import com.zimbra.cs.mailbox.ContactAutoComplete.ContactEntry;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AutoComplete
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  39 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  40 */     Account account = getRequestedAccount(getZimbraSoapContext(context));
/*  41 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  43 */     String name = request.getAttribute("name");
/*     */     
/*     */ 
/*  46 */     name = name.replace(",", " ").trim();
/*     */     
/*  48 */     GalSearchType type = GalSearchType.fromString(request.getAttribute("t", "account"));
/*  49 */     int limit = account.getContactAutoCompleteMaxResults();
/*  50 */     boolean needCanExpand = request.getAttributeBool("needExp", false);
/*     */     
/*  52 */     ContactAutoComplete.AutoCompleteResult result = query(request, zsc, account, false, name, limit, type, needCanExpand, octxt);
/*  53 */     Element response = zsc.createElement(MailConstants.AUTO_COMPLETE_RESPONSE);
/*  54 */     toXML(response, result, zsc.getAuthtokenAccountId());
/*     */     
/*  56 */     return response;
/*     */   }
/*     */   
/*     */   public boolean needsAuth(Map<String, Object> context)
/*     */   {
/*  61 */     return true;
/*     */   }
/*     */   
/*     */   private ArrayList<Integer> csvToArray(String csv) {
/*  65 */     if (csv == null)
/*  66 */       return null;
/*  67 */     ArrayList<Integer> array = new ArrayList();
/*  68 */     for (String f : csv.split(",")) {
/*  69 */       array.add(Integer.valueOf(Integer.parseInt(f)));
/*     */     }
/*  71 */     return array;
/*     */   }
/*     */   
/*     */   protected ContactAutoComplete.AutoCompleteResult query(Element request, ZimbraSoapContext zsc, Account account, boolean excludeGal, String name, int limit, GalSearchType type, OperationContext octxt) throws ServiceException
/*     */   {
/*  76 */     return query(request, zsc, account, excludeGal, name, limit, type, false, octxt);
/*     */   }
/*     */   
/*     */   protected ContactAutoComplete.AutoCompleteResult query(Element request, ZimbraSoapContext zsc, Account account, boolean excludeGal, String name, int limit, GalSearchType type, boolean needCanExpand, OperationContext octxt) throws ServiceException
/*     */   {
/*  81 */     if (!canAccessAccount(zsc, account)) {
/*  82 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*  84 */     ArrayList<Integer> folders = csvToArray(request.getAttribute("folders", null));
/*  85 */     ContactAutoComplete autoComplete = new ContactAutoComplete(account, zsc, octxt);
/*  86 */     autoComplete.setNeedCanExpand(needCanExpand);
/*  87 */     autoComplete.setSearchType(type);
/*  88 */     boolean includeGal = (!excludeGal) && (request.getAttributeBool("includeGal", autoComplete.includeGal()));
/*  89 */     autoComplete.setIncludeGal(includeGal);
/*  90 */     return autoComplete.query(name, folders, limit);
/*     */   }
/*     */   
/*     */   protected void toXML(Element response, ContactAutoComplete.AutoCompleteResult result, String authAccountId) {
/*  94 */     response.addAttribute("canBeCached", result.canBeCached);
/*  95 */     for (ContactAutoComplete.ContactEntry entry : result.entries) {
/*  96 */       Element cn = response.addElement("match");
/*     */       
/*     */ 
/*     */ 
/* 100 */       if (!entry.isContactGroup()) {
/* 101 */         cn.addAttribute("email", entry.getEmail());
/*     */       }
/* 103 */       cn.addAttribute("type", getType(entry));
/* 104 */       cn.addAttribute("ranking", Integer.toString(entry.getRanking()));
/* 105 */       cn.addAttribute("isGroup", entry.isGroup());
/* 106 */       if ((entry.isGroup()) && (entry.canExpandGroupMembers())) {
/* 107 */         cn.addAttribute("exp", true);
/*     */       }
/*     */       
/* 110 */       ItemId id = entry.getId();
/* 111 */       if (id != null) {
/* 112 */         cn.addAttribute("id", id.toString(authAccountId));
/*     */       }
/*     */       
/* 115 */       int folderId = entry.getFolderId();
/* 116 */       if (folderId > 0) {
/* 117 */         cn.addAttribute("l", Integer.toString(folderId));
/*     */       }
/* 119 */       if (entry.isContactGroup()) {
/* 120 */         cn.addAttribute("display", entry.getDisplayName());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private String getType(ContactAutoComplete.ContactEntry entry) {
/* 126 */     if (entry.getFolderId() == 0)
/* 127 */       return "gal";
/* 128 */     if (entry.getFolderId() == -1) {
/* 129 */       return "rankingTable";
/*     */     }
/* 131 */     return "contact";
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/AutoComplete.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */